﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMS.DAL.Providers;
using SMS.Business;
using SMS.DAL;
using SMS.Business.Customer;

namespace SMS.Orders
{
    public partial class OrderSearch : System.Web.UI.Page
    {
        List<Order> orders
        {
            set
            {
                if (ViewState["orders"] != null)
                    ViewState["orders"] = value;
                else
                    ViewState["orders"] = new List<Order>();
            }
            get
            {
                if (ViewState["orders"] != null)
                    return (List<Order>)ViewState["orders"];
                else
                    return new List<Order>();
            }
        }

        private void BindDdlCustomer()
        {
            CustomerBussiness customerBus = new CustomerBussiness();
            List<Customer> customers = customerBus.GetAll();
            customers.Insert(0, new Customer() { ID = 0, Name = "أخــر" });
            ddlCustomers.DataSource = customers;
            ddlCustomers.DataValueField = "ID";
            ddlCustomers.DataTextField = "Name";
            ddlCustomers.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                orders = new List<Order>();
                BindDdlCustomer();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            OrderSearchCriteria orderSearch = new OrderSearchCriteria()
            {
                type = int.Parse(ddlType.SelectedValue)
            };
            if (txtOrderID.Text != string.Empty)
                orderSearch.ID = int.Parse(txtOrderID.Text);
            else
                orderSearch.ID = 0;
            if (txtDateSearch.Text != string.Empty)
                orderSearch.date = DateTime.Parse(txtDateSearch.Text);
            else
                orderSearch.date = null;
            OrderBusiness orderBus = new OrderBusiness();
            orders = orderBus.Search(orderSearch);
            gvOrders.DataSource = orders;
            gvOrders.DataBind();
        }

        protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (orders.Count > 0)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lblType = (Label)e.Row.FindControl("lblType");
                    if (orders[e.Row.RowIndex].Type == 1)
                        lblType.Text = "شــراء";
                    else
                        lblType.Text = "بيــع";
                }
            }
        }

        protected void gvOrders_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "details")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                Response.Redirect("~/orders/OrderDetails.aspx?ID=" + id);
            }
        }

        protected void gvOrders_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = (int)e.Keys[e.RowIndex];
            OrderBusiness orderBus = new OrderBusiness();
            orderBus.Delete(id);
        }

        protected void gvOrders_PageIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrders.PageIndex = e.NewPageIndex;
            gvOrders.DataSource = orders;
            gvOrders.DataBind();
        }
    }
}